package com.empsys.controller;

import com.empsys.pojo.Emp;
import com.empsys.pojo.Result;
import com.empsys.service.EmpService;
import com.empsys.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wzm
 * @create 2023-12-13 9:24
 * project_name:web2310
 * @Description
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private EmpService empService;

    //该接口用于员工登录
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        //调用service中的login方法
        Emp e = empService.login(emp);
        //判断查询的结果
        if(e != null){
            //携带自定义的数据载荷
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());

            //生成令牌
            String jwt = JwtUtils.getJwt(claims);
            return Result.success(jwt);

        }
        return Result.error("用户名或密码错误！");

    }
}
